package com.jiaotong.smart.smartjiaotong.service.subregionconfigservice.serviceiml;

import com.baomidou.mybatisplus.core.conditions.query.Query;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jiaotong.smart.smartjiaotong.entity.subregionconfig.Cross;
import com.jiaotong.smart.smartjiaotong.entity.subregionconfig.SubSearch;
import com.jiaotong.smart.smartjiaotong.entity.subregionconfig.Subregion;
import com.jiaotong.smart.smartjiaotong.mapper.subregionconfigmapper.CrossMapper;
import com.jiaotong.smart.smartjiaotong.mapper.subregionconfigmapper.SubSearchMapper;
import com.jiaotong.smart.smartjiaotong.mapper.subregionconfigmapper.SubregionMapper;
import com.jiaotong.smart.smartjiaotong.service.subregionconfigservice.SubRegionservice;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.*;

/**
 * @Author 文帅
 * 子区配置模块service各个方法实现
 */
@Service
public class SubRegionserviceiml implements SubRegionservice {


    @Autowired
    CrossMapper crossMapper;

    @Autowired
    SubregionMapper subregionMapper;

    @Autowired
    SubSearchMapper subSearchMapper;

    @Autowired
    private RedisTemplate<Serializable,Object> redisTemplate;

    /**
     * 搜索记录
     * 无参数
     * @return 所有查找记录
     */
    @Override
    public List<String> getsearchrecords() {
        List<String> searchlist = new ArrayList<>();
        String key = "research";
        Set<ZSetOperations.TypedTuple<Object>> reserchcord = redisTemplate.opsForZSet().reverseRangeWithScores(key, 0, 10);
        Iterator<ZSetOperations.TypedTuple<Object>> iterator = reserchcord.iterator();
        while(iterator.hasNext()){
            ZSetOperations.TypedTuple<Object> next = iterator.next();
            if(next.getValue()!=null){
                searchlist.add(next.getValue().toString());
            }
        }
        return searchlist;
    }

    /**
     * 所有子区
     * 无参数
     * @return 所有子区
     */
    @Override
    public List<Subregion> getallsubregion() {
        return subregionMapper.selectList(null);
    }

    /**
     * 返回所有属于该区域的子区
     * @param regionid 区域id
     * @return 对应子区
     */
    @Override
    public List<Subregion> getallsubregionbyregionid(int regionid){
        QueryWrapper<Subregion> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("regionid", regionid);
        List<Subregion> subregions = subregionMapper.selectList(queryWrapper);
        return subregions;
    }

    /**
     * 在线、离线子区数量
     * @return int[0]:在线数量；
     *         int[1]:离线数量；
     */
    @Override
    public long[] getnumber() {
        long[] numbers = new long[2];
        QueryWrapper<Subregion> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ison",1);
        long ison = subregionMapper.selectCount(queryWrapper);
        System.out.println(ison);
        numbers[0] = ison;

        QueryWrapper<Subregion> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("ison",0);
        long isdown = subregionMapper.selectCount(queryWrapper1);
        System.out.println(isdown);
        numbers[1] = isdown;

        return numbers;
    }

    /**
     *搜索子区（模糊搜索）
     * @return 所有匹配的子区列表
     */
    @Override
    public List<Subregion> getsubregionbyname(String name) {
        QueryWrapper<Subregion> queryWrapper = new QueryWrapper<>();
        QueryWrapper<Subregion> subregionname = queryWrapper
                .like(Strings.isNotBlank(name), "subregionname", name);
        List<Subregion> subregions = subregionMapper.selectList(queryWrapper);
        addsearchrecord(name);
        return subregions;
    }

    /**
     *插入搜索记录
     * @return 是否插入成功
     *         1：成功
     *         0：失败
     */
    @Override
    public int addsearchrecord(String name) {

//        SubSearch subSearch = new SubSearch();
//        subSearch.setSearchname(name);
//        subSearch.setSearchtime(new Date(System.currentTimeMillis()));
//        int insert = subSearchMapper.insert(subSearch);

        String key = "research";
        redisTemplate.opsForZSet().add(key,name,System.currentTimeMillis());
        long top = 10;
        Double research = redisTemplate.opsForZSet().score(key, name);
        if(research!=null)
            redisTemplate.opsForZSet().remove(key,name);
        redisTemplate.opsForZSet().add(key,name,System.currentTimeMillis());

        Long research1 = redisTemplate.opsForZSet().zCard(key);
        if(research > top){
            redisTemplate.opsForZSet().removeRange(key,0,research1-top-1);
        }
        return 1;
    }

    /**
     *添加新子区（需重新考虑错误信息）
     * @return 子区知否插入成功
     */
    @Override
    public int addsubregion(Subregion subregion, List<Cross> crosses) {
        int insert = subregionMapper.insert(subregion);
        for (Cross cross : crosses) {
            crossMapper.insert(cross);
        }
        return insert;
    }
}
